const util = require('../../utils/util.js')
Page({
  data: {
    _id: '',
    startDate: '',
    endDate: '',
    courseName: '',
    classes: '',
    examDate: '',
    startTime: '',
    endTime: '',
    examAddress: '',
    teacher: ''
  },
  initDate() {
    this.setData({
      startDate: util.startDate(),
      endDate: util.endDate()
    })
  },
  onLoad: function (options) {
    this.setData({
      _id: options._id
    })
    this.initDate()
    this.getExamById()
  },
  getExamById: function() {
    wx.showLoading({
      title: '加载中',
    })
    var that = this
    wx.cloud.callFunction({
      name: 'getExamById',
      data: {
        _id: this.data._id
      },
      success: function (res) {
        wx.hideLoading()
        that.setData({
          courseName: res.result.data.courseName,
          classes: res.result.data.classes,
          examDate: res.result.data.examDate,
          startTime: res.result.data.startTime,
          endTime: res.result.data.endTime,
          examAddress: res.result.data.examAddress,
          teacher: res.result.data.teacher
        })
      },
      fail: function (res) {
        console.log(res)
      }
    })
  },
  setCourseName: function (e) {
    this.setData({
      courseName: e.detail.value
    })
  },
  setClasses: function (e) {
    this.setData({
      classes: e.detail.value
    })
  },
  setDate: function (e) {
    this.setData({
      examDate: e.detail.value
    })
  },
  setStartTime: function (e) {
    this.setData({
      startTime: e.detail.value
    })
  },
  setEndTime: function (e) {
    this.setData({
      endTime: e.detail.value
    })
  },
  setExamAddress: function (e) {
    this.setData({
      examAddress: e.detail.value
    })
  },
  setTeacher: function (e) {
    this.setData({
      teacher: e.detail.value
    })
  },
  save: function() {
    if (this.data.courseName == '') {
      wx.showToast({
        title: '考试科目必须填写',
        icon: 'none'
      })
      return
    }
    if (this.data.classes == '') {
      wx.showToast({
        title: '考试班级必须填写',
        icon: 'none'
      })
      return
    }
    if (this.data.startTime == '') {
      wx.showToast({
        title: '考试开始时间必须填写',
        icon: 'none'
      })
      return
    }
    if (this.data.endTime == '') {
      wx.showToast({
        title: '考试结束时间必须填写',
        icon: 'none'
      })
      return
    }
    if (this.data.startTime >= this.data.endTime) {
      wx.showToast({
        title: '考试结束时间必须大于开始时间',
        icon: 'none'
      })
      return
    }
    if (this.data.examAddress == '') {
      wx.showToast({
        title: '考试地点必须填写',
        icon: 'none'
      })
      return
    }
    if (this.data.teacher == '') {
      wx.showToast({
        title: '监考教师必须填写',
        icon: 'none'
      })
      return
    }
    wx.showLoading({
      title: '保存中',
    })
    wx.cloud.callFunction({
      name: 'updateExam',
      data: {
        _id: this.data._id,
        courseName: this.data.courseName,
        classes: this.data.classes,
        examDate: this.data.examDate,
        startTime: this.data.startTime,
        endTime: this.data.endTime,
        examAddress: this.data.examAddress,
        teacher: this.data.teacher,
        updated_at: util.datetime()
      },
      success: function(res) {
        wx.hideLoading()
        wx.showToast({
          title: '保存成功',
          success: function() {
            wx.redirectTo({
              url: '../manageExam/manageExam',
            })
          }
        })
      },
      fail: function(res) {
        console.log(res)
      }
    })
  }
})